﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using _3DGame.Components;
using OneLine.Components;
using OneLine2.Components;

namespace _3DGame.Models
{
    class BasicModel
    {
        public Model model { get; protected set; }
        protected Matrix world;
        Vector3 cameraPosition;
        Matrix cameraOrientation;
        float yOffSet = 0;
        float xOffSet = 0;
        float zOffSet = 0;
        public BasicModel(Model m, float yOffSet = 0, float xOffSet = 0, float zOffSet = 0)
        {
            model = m;
            this.xOffSet = xOffSet;
            this.yOffSet = yOffSet;
            this.zOffSet = zOffSet;
        }

        public void SetCameraDetails(CameraComponent camera)
        {
            cameraPosition = camera.Position;
            cameraOrientation = Matrix.CreateFromQuaternion(Quaternion.Inverse(camera.Orientation));
            
        }

        public virtual void Update(GameTime time, CameraComponent camera)
        {

        }

        public void Draw(GameTime time, CameraComponent camera)
        {
            
            
            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect be in mesh.Effects)
                {
                    
                    

                    be.EnableDefaultLighting();
                    be.Projection = camera.ProjectionMatrix;
                    be.View = camera.ViewMatrix;
                    Matrix world = cameraOrientation * transforms[0];

                    
                    //world.
                    world.M41 = cameraPosition.X + xOffSet;
                    world.M42 = cameraPosition.Y + yOffSet;
                    world.M43 = cameraPosition.Z + zOffSet;
                    be.World = world;
                    
                    
                }
                mesh.Draw();
            }
        }
        

    }
}
